#!/bin/awk -f
## used by thfit
BEGIN{
  fn="tham";
  while(getline<fn){
    if($1=="coef"){
      k++;
      c[$4]=$3;
      cc[k]=$4;
    }
    if($1=="datapoints"){print $0};
    if($1=="parameters"){print $0};
    if($1=="rms_atom"){print $0};
  }
  fn="thfit.out";
  while(getline<fn){
    de[$6]=$4;
  }
  for(i=1;i<=k;i++){
    printf("coef %8.3f %s\n",c[cc[i]],cc[i]);
  }
  ARGV[1]="thfit.inp";
  ARGC=2;
}
FNR==1{nc=$1;for(i=3;i<$2+3;i++){p[$i]++}}
FNR==2{x=$(NF);for(i=1;i<=nc;i++){if(p[i]==""){m++;s[i]=$i;printf("%3s ",$i)}};printf " TH-VASP TNAME\n"}
FNR>2{x=$(NF);for(i=1;i<=nc;i++){if(p[i]==""){ss=ss""sprintf("%3i ",$i)}};ss=ss""sprintf("%7.1f %s\n",-de[x]*1000,x)};
END{
  printf ss | "sort -n +"m;
}
